<template>
	<div class="app-container">
		<!-- 高级查询 -->
		<el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">


			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<!-- 工具栏 -->
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tbWmsMaterialReceiptLine:insert']" type="primary" icon="el-icon-plus"
					size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tbWmsMaterialReceiptLine:update']" type="success" icon="el-icon-edit"
					size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tbWmsMaterialReceiptLine:delete']" type="danger" icon="el-icon-delete"
					size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
			</el-col>
			
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tbWmsMaterialReceiptLine:export']" type="warning"
					icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
			</el-col>
			<right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
		</el-row>

		<!-- 数据列表 -->
		<el-table v-loading="loading" :data="page.records" highlight-current-row
			@selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="props" style="padding: 20px 50px;">
          <el-table v-loading="loading" :data="props.row.itemList" highlight-current-row>

            <el-table-column label="所在库位" align="center" prop="locationNo" width="150px" />
            <el-table-column label="商品名称" align="center" prop="materialName" width="150px" />
            <el-table-column label="数量" align="center" prop="quantity" width="150px" />

          </el-table>
        </template>
      </el-table-column>
			<el-table-column label="入库单号" align="center" prop="orderNo" />
      <el-table-column label="二维码" align="center" prop="createTime" width="200">
      	<template slot-scope="scope">
      		<div :id="'qrCode-' + scope.$index">
      			<!-- {{scope.$index}} -->
      			<vue-qr
      				:text="scope.row.id + ',' + scope.row.customerName + ',' + scope.row.customerNo + ',' + scope.row.quantity"
      				:size="180"></vue-qr>
      		</div>

      	</template>
      </el-table-column>
			<el-table-column label="仓库编号" align="center" prop="warehouseCode" />
			<el-table-column label="客户编号" align="center" prop="customerNo" />
			<el-table-column label="客户名称" align="center" prop="customerName" />
			<el-table-column label="状态" align="center" prop="status" />
			<el-table-column label="预入库区域" align="center" prop="consignmentLocation" />
			<el-table-column label="预入库时间" align="center" prop="preArrivedTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.preArrivedTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="备注" align="center" prop="remark" />


			<el-table-column label="入库模板编码" align="center" prop="templateId" />

			<el-table-column label="来源" align="center" prop="sourceType" />
			<el-table-column label="来源单号" align="center" prop="sourceOrderNo" />
			<el-table-column label="与入库卸货站台" align="center" prop="stageNo" />

			<el-table-column label="登记时间" align="center" prop="createTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>

			<el-table-column width="200" label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button v-hasPermission="['web:tbWmsMaterialReceipt:update']" size="mini" type="text"
						icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
					<el-button v-hasPermission="['web:tbWmsMaterialReceipt:delete']" size="mini" type="text"
						icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
					<el-button size="small" type="primary" icon="el-icon-plus" @click="handlePrint(scope.$index)">打印
					</el-button>
					<el-button size="small" type="primary" icon="el-icon-plus" @click="handleView(scope.row)">详情
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 分页 -->
		<pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize"
			@pagination="handleQuery" />
		<el-dialog :title="inLocDialog.title" :visible.sync="inLocDialog.visible" :close-on-click-modal="false"
			width="800px" append-to-body>
      <el-form ref="inLocDialogForm" :model="inLocDialogData"
      	:rules="inLocDialogFormRules" label-width="120px">
		   <el-form-item label="是否需要分拆" prop="needRePackYn">

              <el-checkbox v-model="inLocDialogData.needRePackYn" false-label="0" true-label="1" />

				</el-form-item>
  <el-form-item label="是否需要托盘" prop="needPalletYn">

   <el-checkbox v-model="inLocDialogData.needPalletYn" false-label="0" true-label="1" />
				</el-form-item>
        <el-form-item label="是否需要叉车" prop="needLiftYn">

         <el-checkbox v-model="inLocDialogData.needLiftYn" false-label="0" true-label="1" />
        			</el-form-item><</el-form>
			<el-table v-loading="eloading" :data="employeePage.erecords" highlight-current-row
				@selection-change="handleEmployeeSelectionChange">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="选择收货人员" align="center" prop="personName" />
				<el-table-column label="收货人员编号" align="center" prop="userId" />
				<el-table-column label="" align="center" prop="userType" />
				<el-table-column label="" align="center" prop="enrollDate" width="180">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.enrollDate, '{y}-{m}-{d}') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="" align="center" prop="currentJobLoad" />
				<el-table-column label="" align="center" prop="idCard" />
			</el-table>
			<pagination v-show="employeePage.total>0" :total="employeePage.total" :page.sync="employeePage.current"
				:limit.sync="employeePage.pageSize" @pagination="handleEmployeeQuery" />

        <el-table v-loading="lloading" :data="employeeLiftPage.lrecords" highlight-current-row
        	@selection-change="handleEmployeeLiftSelectionChange">
        	<el-table-column type="selection" width="55" align="center" />
        	<el-table-column label="选择叉车驾驶员" align="center" prop="personName" />
        	<el-table-column label="叉车驾驶员编号" align="center" prop="userId" />
        	<el-table-column label="" align="center" prop="userType" />
        	<el-table-column label="" align="center" prop="enrollDate" width="180">
        		<template slot-scope="scope">
        			<span>{{ parseTime(scope.row.enrollDate, '{y}-{m}-{d}') }}</span>
        		</template>
        	</el-table-column>
        	<el-table-column label="" align="center" prop="currentJobLoad" />
        	<el-table-column label="" align="center" prop="idCard" />
        </el-table>
        <pagination v-show="employeeLiftPage.total>0" :total="employeeLiftPage.total" :page.sync="employeeLiftPage.current"
        	:limit.sync="employeeLiftPage.pageSize" @pagination="handleEmployeeLiftQuery" />
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="confirmInLocDialog">确 定</el-button>
				<el-button @click="resetInLocDialog">取 消</el-button>
			</div>
		</el-dialog>
		<!-- 添加/修改对话框 -->
		<el-dialog :title="tbWmsMaterialReceiptFormDialog.title" :visible.sync="tbWmsMaterialReceiptFormDialog.visible"
			:close-on-click-modal="false" width="500px" append-to-body>
			<el-form ref="tbWmsMaterialReceiptForm" :model="tbWmsMaterialReceiptFormData"
				:rules="tbWmsMaterialReceiptFormRules" label-width="80px">
				<el-form-item label="" prop="sourceOrderNo">
					<el-input v-model="tbWmsMaterialReceiptFormData.sourceOrderNo" />
					<el-col :span="1.5" style="float: right;">

						<el-button style="float: right;" type="primary" icon="el-icon-plus" size="mini"
							@click="openContractSelector">选择需要入库的订单</el-button>
						<purchase-order-selector :visible.sync="contractSelectorVisible"
							@selected="handleSelectedContract" />

					</el-col>
				</el-form-item>


				<el-form-item label="仓库" prop="warehouseCode">
					<el-input v-model="tbWmsMaterialReceiptFormData.warehouseCode" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="客户编号" prop="customerNo">
					<el-input v-model="tbWmsMaterialReceiptFormData.customerNo" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="客户名称" prop="customerName">
					<el-input v-model="tbWmsMaterialReceiptFormData.customerName" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="预入库区域" prop="consignmentLocation">
					<el-input v-model="tbWmsMaterialReceiptFormData.consignmentLocation" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="预入库时间" prop="preArrivedTime">
					<el-date-picker v-model="tbWmsMaterialReceiptFormData.preArrivedTime" type="date"
						value-format="yyyy-MM-dd" size="small" style="width: 200px" placeholder="选择" clearable />
				</el-form-item>
				<el-form-item label="卸货站台" prop="stageNo">
					<el-input v-model="tbWmsMaterialReceiptFormData.stageNo" placeholder="请输入" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="confirmTbWmsMaterialReceiptFormDialog">确 定</el-button>
				<el-button @click="resetTbWmsMaterialReceiptFormDialog">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import TWmsReciptIntoLocPlanApi from '@/api/web/TWmsReciptIntoLocPlanApi'
	import TbWmsMaterialReceiptApi from '@/api/web/TbWmsMaterialReceiptApi'
	import PurchaseOrderSelector from '@/views/order/purchaseOrder/PurchaseOrderSelector'
	import HrEmployeeManageApi from '@/api/basic/HrEmployeeManageApi'
	import vueQr from 'vue-qr'
	import printJS from 'print-js'
	const defaultTbWmsMaterialReceiptFormData = {
		orderNo: null,
		warehouseCode: null,
		customerNo: null,
		customerName: null,
		siteCode: null,
		status: '0',
		consignmentLocation: null,
		preArrivedTime: null,
		version: null,
		receiptNo: null,
		templateId: null,
		quantity: null,
		sourceType: null,
		sourceOrderNo: null,
		stageNo: null,
	}
	export default {
		name: 'TbWmsMaterialReceiptList',
		components: {
			PurchaseOrderSelector,
			vueQr
		},
		data() {
			return {
				options: {
					sysYnTypeOptions: this.$constant.sysYnTypeOptions
				},
				contractSelectorVisible: false,
				queryParams: {
					warehouseCode: null,
					customerNo: null,
					siteCode: null,
                    userType: null,
					status: null,
					consignmentLocation: null,
					preArrivedTime: null,
					remark: null,
					version: null,
					receiptNo: null,
					templateId: null,
					quantity: null,
					sourceType: null,
					sourceOrderNo: null,
					stageNo: null,
					isValid: this.$constant.Y
				},
        equeryParams: {
					warehouseCode: null,
					customerNo: null,
          userType: null,
					siteCode: null,
					status: null,
					consignmentLocation: null,
					preArrivedTime: null,
					remark: null,
					version: null,
					receiptNo: null,
					templateId: null,
					quantity: null,
					sourceType: null,
					sourceOrderNo: null,
					stageNo: null,
					isValid: this.$constant.Y
				},
				page: {
					current: 1,
					pageSize: 10,
					total: 0,
					records: []
				},
				employeePage: {
					current: 1,
					pageSize: 10,
					total: 0,
					erecords: []
				},
        employeeLiftPage: {
        	current: 1,
        	pageSize: 10,
        	total: 0,
        	lrecords: []
        },

				tbWmsMaterialReceiptFormDialog: {
					title: '',
					visible: false
				},
				inLocDialog: {
					title: '生成收货上架任务单',
					visible: false
				},
				inLocDialogData: {
					inLocUserNo: null,
          needRePackYn: null,
          needLiftYn: null,
          liftEmployees: [],
          pickEmployees: [],
          needPalletYn: null
				},
				tbWmsMaterialReceiptFormData: Object.assign({}, defaultTbWmsMaterialReceiptFormData),
				tbWmsMaterialReceiptFormRules: {
					warehouseCode: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
					customerNo: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
					siteCode: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
					status: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
					preArrivedTime: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
					version: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
				},
				loading: true,
				eloading: true,
        lloading: true,
				searchVisible: true,
				multipleSelection: [],
        lemployeeSelecMultipleSelection: [],
        employeeSelecMultipleSelection: [],
				isSingle: true,
				isMultiple: true,
        eisSingle: true,
        eisMultiple: true,
        lisSingle: true,
        lisMultiple: true
			}
		},
		created() {
			this.handleQuery()
			this.handleEmployeeQuery()
      this.handleEmployeeLiftQuery()
			this.loadOptions()
		},
		methods: {
			loadOptions() {},
			handlePrint(index) {
				console.log(index)
				let userAgent = navigator.userAgent;
				let printableId = 'qrCode-' + index;
				console.log(printableId)
				//判断是否Firefox浏览器
				if (userAgent.indexOf("Firefox") > -1) {
					console.log('Firefox')
					printJS({
						printable: printableId, // 标签元素id
						type: 'html',
						header: '',
						style: `@page {size:auto;margin-top:100px; margin-left:15px; margin-right: 15px;}
			            thead th {
			              border-top: 1px solid #000;
			              border-right: 1px solid #000;
			              border-left: 1px solid #000;
			            }
			            tbody td {
			              border: 1px solid #000;
			            }
			            tbody {
			              text-align: center;
			            }
			            table {
			              border-collapse: collapse;
			            }`,
					});
				}
				//判断是否chorme浏览器
				if (userAgent.indexOf("Chrome") > -1) {
					console.log('Chrome')
					printJS({
						printable: printableId, // 标签元素id
						type: 'html',
						header: '',
						documentTitle: '',
						style: `@page {size:auto;margin-top:50%; margin-left:40%;}
			            thead th {
			              border-top: 1px solid #000;
			              border-right: 1px solid #000;
			              border-left: 1px solid #000;
			            }
			            tbody td {
			              border: 1px solid #000;
			            }
			            tbody {
			              text-align: center;
			            }
			            table {
			              border-collapse: collapse;
			            }`,
					});
				}
				//判断是否IE浏览器
				if (!!window.ActiveXObject || "ActiveXObject" in window) {
					console.log('IE')
					printJS({
						printable: printableId, // 标签元素id
						type: 'html',
						header: '',
						style: `@page {size:auto;margin-top:100px; margin-left:15px; margin-right: 15px;}
			            thead th {
			              border-top: 1px solid #000;
			              border-right: 1px solid #000;
			              border-left: 1px solid #000;
			            }
			            tbody td {
			              border: 1px solid #000;
			            }
			            tbody {
			              text-align: center;
			            }
			            table {
			              border-collapse: collapse;
			            }`,
					});
				}
			},
			openContractSelector() {
				this.contractSelectorVisible = true
			},
			handleSelectedContract() {

			},
			handleView(row) {
				this.$router.push({
					path: `/tbWmsMaterialReceipt/view/${row.id}`
				})
			},
			handleEmployeeQuery() {
				this.eloading = true
        this.equeryParams.userType = 'pick'
				HrEmployeeManageApi.page(this.equeryParams, this.employeePage).then(response => {
					this.employeePage.erecords = response.data.records
					this.employeePage.total = response.data.total
					this.eloading = false
				}, () => {
					this.eloading = false
				})
			},
      handleEmployeeLiftQuery() {
      	this.lloading = true
         this.equeryParams.userType = 'lift'
      	HrEmployeeManageApi.page(this.equeryParams, this.employeeLiftPage).then(response => {
      		this.employeeLiftPage.lrecords = response.data.records
      		this.employeeLiftPage.total = response.data.total
      		this.lloading = false
      	}, () => {
      		this.lloading = false
      	})
      },

			handleAdd() {
				this.openTbWmsMaterialReceiptFormDialog()
			},
			handleAutoInLoc() {
				TWmsReciptIntoLocPlanApi.inLocPlan().then(response => {
					this.$message.success('上架计划生成成功')
					this.handleQuery()
				})
			},
			handleInLoc(row) {
				const id = row.id || this.multipleSelection.map(item => item.id)[0]
				this.openInLocDialog(id)
			},
			openInLocDialog(id) {
				this.inLocDialog.visible = true
			},
			confirmInLocDialog() {

        this.inLocDialogData.liftEmployees = this.lemployeeSelecMultipleSelection.map(item => item.id)
				this.inLocDialogData.pickEmployees = this.employeeSelecMultipleSelection.map(item => item.id)

        console.log(params)
        	const ids = this.multipleSelection.map(item => item.id)
				TWmsReciptIntoLocPlanApi.inLocPlanBySelect(this.inLocDialogData,ids).then(response => {
					this.$message.success('上架计划生成成功')
				})
        this.inLocDialog.visible = false
			},
			resetInLocDialog() {
				this.inLocDialog.visible = false
			},
			handleRemove(row) {
				const ids = row.id || this.multipleSelection.map(item => item.id)
				this.$confirm('是否确认删除数据?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					return TbWmsMaterialReceiptApi.remove(ids)
				}).then(() => {
					this.handleQuery()
					this.$message.success('删除成功')
				})
			},
			handleEdit(row) {
				const id = row.id || this.multipleSelection.map(item => item.id)[0]
				this.openTbWmsMaterialReceiptFormDialog(id)
			},
			handleQuery() {
				this.loading = true
				TbWmsMaterialReceiptApi.page(this.queryParams, this.page).then(response => {
					this.page.records = response.data.records
					this.page.total = response.data.total
					this.loading = false
				}, () => {
					this.loading = false
				})
			},
			handleExport() {
				this.$confirm('是否确认导出数据?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					return TbWmsMaterialReceiptApi.exportData(this.queryParams)
				}).then(response => {
					this.$common.download(response.msg)
				})
			},
			openTbWmsMaterialReceiptFormDialog(id) {
				this.resetTbWmsMaterialReceiptFormDialog()
				this.tbWmsMaterialReceiptFormDialog.visible = true
				if (id) {
					this.tbWmsMaterialReceiptFormDialog.title = '入库计划-修改'
					TbWmsMaterialReceiptApi.get(id).then(response => {
						this.tbWmsMaterialReceiptFormData = {
							...this.tbWmsMaterialReceiptFormData,
							...response.data
						}
					})
				} else {
					this.tbWmsMaterialReceiptFormDialog.title = '入库计划-添加'
				}
			},
			confirmTbWmsMaterialReceiptFormDialog() {
				this.$refs['tbWmsMaterialReceiptForm'].validate(valid => {
					if (valid) {
						if (this.tbWmsMaterialReceiptFormData.id) {
							TbWmsMaterialReceiptApi.edit(this.tbWmsMaterialReceiptFormData).then(response => {
								this.$message.success('修改成功')
								this.tbWmsMaterialReceiptFormDialog.visible = false
								this.handleQuery()
							})
						} else {
							TbWmsMaterialReceiptApi.add(this.tbWmsMaterialReceiptFormData).then(response => {
								this.$message.success('添加成功')
								this.tbWmsMaterialReceiptFormDialog.visible = false
								this.handleQuery()
							})
						}
					}
				})
			},
			resetTbWmsMaterialReceiptFormDialog() {
				this.tbWmsMaterialReceiptFormDialog = {
					title: '',
					visible: false
				}
				this.tbWmsMaterialReceiptFormData = Object.assign({}, defaultTbWmsMaterialReceiptFormData)
				this.resetForm('tbWmsMaterialReceiptForm')
			},
			handleSelectionChange(selection) {
				this.multipleSelection = selection
				this.isSingle = selection.length !== 1
				this.isMultiple = !selection.length
			},
      handleEmployeeSelectionChange(selection) {
      	this.employeeSelecMultipleSelection = selection
      	this.eisSingle = selection.length !== 1
      	this.eisMultiple = !selection.length
      },
      handleEmployeeLiftSelectionChange(selection) {
      	this.lemployeeSelecMultipleSelection = selection
      	this.lisSingle = selection.length !== 1
      	this.lisMultiple = !selection.length
      },
			resetQuery() {
				this.page.current = 1
				this.resetForm('queryForm')
				this.handleQuery()
			}
		}
	}
</script>
